﻿package idv.cjcat.monica.platformer.player {
	import idv.cjcat.monica.box2d.Box2DCategory;
	import idv.cjcat.rusher.box2d.Box2DBodyComponent;
	import idv.cjcat.rusher.box2d.Box2DBodyType;
	import idv.cjcat.rusher.box2d.fixtures.BoxFixtureDef;
	import idv.cjcat.rusher.box2d.fixtures.CircleFixtureDef;
	import idv.cjcat.rusher.box2d.fixtures.IFixtureDef;
	import idv.cjcat.rusher.box2d.fixtures.OrientedBoxFixtureDef;
	import idv.cjcat.rusher.box2d.fixtures.PolygonFixtureDef;
	import idv.cjcat.rusher.geom.Vec2D;
	
	public class PlayerSpatialComponent extends Box2DBodyComponent {
		
		private static const SENSOR_WIDTH:Number = 16;
		private static const SENSOR_HEIGHT:Number = 40;
		
		private static const COLLIDER_ANGLE:Number = 45;
		private static const COLLIDER_RADIUS:Number = 3;
		private static const FOOT_COLLIDER_POS:Vec2D = new Vec2D(0, -COLLIDER_RADIUS);
		private static const HEAD_COLLIDER_POS:Vec2D = new Vec2D(0, -SENSOR_HEIGHT + COLLIDER_RADIUS);
		private static const RIGHT_COLLIDER_POS:Vec2D = new Vec2D(0.5 * SENSOR_WIDTH - COLLIDER_RADIUS, -0.5 * SENSOR_HEIGHT);
		private static const LEFT_COLLIDER_POS:Vec2D = new Vec2D( -0.5 * SENSOR_WIDTH + COLLIDER_RADIUS, -0.5 * SENSOR_HEIGHT);
		
		
		override public function get position():Vec2D { return super.position; }
		override public function set position(value:Vec2D):void {
			super.position = value;
		}
		
		public function PlayerSpatialComponent() {
			
			fixedRotation = true;
			type = Box2DBodyType.DYNAMIC;
			isBullet = true;
			allowSleep  = false;
			linearDamping = 0.1;
			
			var footCollider:IFixtureDef = new PolygonFixtureDef([ -0.5 * SENSOR_WIDTH + 0.25, -COLLIDER_RADIUS, 0.5 * SENSOR_WIDTH - 0.25, -COLLIDER_RADIUS, 0.15 * SENSOR_WIDTH, 0, -0.15 * SENSOR_WIDTH, 0]);
			//var footCollider:IFixtureDef = new OrientedBoxFixtureDef(0.5 * (SENSOR_WIDTH) - 1, COLLIDER_RADIUS, FOOT_COLLIDER_POS, 0);
			//var footCollider:IFixtureDef = new CircleFixtureDef(COLLIDER_RADIUS, FOOT_COLLIDER_POS);
			footCollider.friction = 0.5;
			footCollider.categoryBits = Box2DCategory.FLOOR;
			footCollider.maskBits = Box2DCategory.FLOOR;
			var footSensor:IFixtureDef = new OrientedBoxFixtureDef(COLLIDER_RADIUS, COLLIDER_RADIUS, new Vec2D(0, COLLIDER_RADIUS), 0);
			footSensor.isSensor = true;
			footSensor.categoryBits = footSensor.maskBits = Box2DCategory.FLOOR;
			createFixture(footSensor);
			
			var headCollider:IFixtureDef = new OrientedBoxFixtureDef(0.5 * SENSOR_WIDTH, COLLIDER_RADIUS, HEAD_COLLIDER_POS);
			headCollider.friction = 0.5;
			headCollider.categoryBits = Box2DCategory.ROOF;
			headCollider.maskBits = Box2DCategory.ROOF;
			
			var rightCollider:IFixtureDef = new OrientedBoxFixtureDef(COLLIDER_RADIUS, (0.5 * SENSOR_HEIGHT) - COLLIDER_RADIUS, RIGHT_COLLIDER_POS);
			rightCollider.friction = 0.5;
			rightCollider.userData = 1;
			rightCollider.categoryBits = Box2DCategory.WALL;
			rightCollider.maskBits = Box2DCategory.WALL;
			
			var leftCollider:IFixtureDef = new OrientedBoxFixtureDef(COLLIDER_RADIUS, (0.5 * SENSOR_HEIGHT) - COLLIDER_RADIUS, LEFT_COLLIDER_POS);
			leftCollider.friction = 0.5;
			leftCollider.userData = -1;
			leftCollider.categoryBits = Box2DCategory.WALL;
			leftCollider.maskBits = Box2DCategory.WALL;
			
			var sensor:IFixtureDef = new OrientedBoxFixtureDef(0.5 * SENSOR_WIDTH, 0.5 * SENSOR_HEIGHT, new Vec2D(0, -0.5 * SENSOR_HEIGHT));
			sensor.categoryBits = Box2DCategory.SENSOR;
			sensor.maskBits = Box2DCategory.SENSOR;
			sensor.isSensor = true;
			
			createFixture(footCollider);
			createFixture(headCollider);
			createFixture(rightCollider);
			createFixture(leftCollider);
			createFixture(sensor);
		}
	}
}